package com.hs.sfzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hs.sfzy.entity.Course;
import com.hs.sfzy.entity.User;
import com.hs.sfzy.entity.UserCourse;
import com.hs.sfzy.mapper.CourseMapper;
import com.hs.sfzy.mapper.UserCourseMapper;
import com.hs.sfzy.mapper.UserMapper;
import com.hs.sfzy.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @BelongsProject: sfzyBackEnd
 * @BelongsPackage: com.hs.sfzy.service.impl
 * @ClassName UserServiceImpl
 * @Author: 陈千鱼不是鱼
 * @CreateTime: 2024-10-24  19:29
 * @Description: 用户服务层
 * @Version: 1.0
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;
    private final UserCourseMapper userCourseMapper;
    private final CourseMapper courseMapper;


    @Override
    public List<User> getIdAndUsername() {
        return userMapper.getUserIdAndName();
    }

    /**
     * 根据id获取用户信息
     * @author QinKunPeng
     * @date 2024/10/24 20:28
     * @param id
     * @return com.hs.sfzy.entity.User
     */
    @Override
    public User getUserById(long id) {
        return userMapper.getUserById(id);
    }

    @Override
    public int insert(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int update(User user) {
        return userMapper.update(user);
    }

    @Override
    public int delete(Long id) {
        return userMapper.delete(id);
    }

    @Override
    public List<User> selectAll(User user) {
        return userMapper.selectAll(user);
    }

    @Override
    public PageInfo<User> pageInfo(int pageNum, int pageSize, User user) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> users = userMapper.selectAll(user);
        for (User us : users){
            List<Course> courses = new ArrayList<Course>();
            Long id = us.getId();
            List<UserCourse> userCourses = userCourseMapper.selectByUserId(id);
            for (UserCourse userCourse : userCourses){
                long courseId = userCourse.getCourseId();
                Course course = courseMapper.selectById(courseId);
                courses.add(course);
                us.setCourses(courses);
            }
        }
        PageInfo<User> pageInfo = new PageInfo<User>(users);
        return pageInfo;
    }

    @Override
    public User login(String username, String password) {
        return userMapper.selectByUsernameAndPassword(username,password);
    }

    @Override
    public Object getUserCourseById(int pageNum, int pageSize, long id) {
        User user = userMapper.getUserById(id);
        System.out.println(id);
        if (user != null){
            PageHelper.startPage(pageNum, pageSize);
            List<UserCourse> userCourses = userCourseMapper.selectByUserId(id);
            System.out.println(userCourses);
            if (userCourses.size() > 0){
                userCourses.forEach(userCourse->{
                    Course course = courseMapper.selectById(userCourse.getCourseId());
                    userCourse.setCourse(course);
                });
            }
            Map map = new HashMap();
            map.put("user", user);
            map.put("userCourse", new PageInfo<UserCourse>(userCourses));
//            user.setUserCourse(userCourses);
//            List<User> list = new ArrayList<>();
//            list.add(user);
//            PageInfo<User> pageInfo = new PageInfo<>(list);
            return map;
        }

        return null;
    }
}
